package com.syni.mdd.yhd.common.all.listen.comment;

import com.syni.mdd.yhd.common.all.constants.AuroraPushCode;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.*;
import com.syni.mdd.yhd.common.all.listen.groupBuy.GroupBuyEvent;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.yhd.common.all.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
public class GenerayCommentListener implements ApplicationListener<CommentEvent> {
    private final static String MODEL = "generalComment";
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsUserVideoService bmsUserVideoService;

    @Autowired
    BmsDxNewsService bmsDxNewsService;


    @Autowired
    BmsBusinessService bmsBusinessService;

    @Override
    @Async
    public void onApplicationEvent(CommentEvent commentEvent) {
        String model = commentEvent.getSource().toString();
        if(!MODEL.equals(model)){
            return;
        }
        BmsVideo bmsVideo = commentEvent.getBmsVideo();
        BmsDxComment bmsDxComment = commentEvent.getBmsDxComment();
        Integer userId = commentEvent.getUserId();
        //普通评论
        //后台发布的视频不记录
        if(bmsVideo.getFromOrigin() == Constants.COMMON_FLAG_FALSE_INT){
            return;
        }
        //获取视频发布者
        BmsUserVideo bmsUserVideo = bmsUserVideoService.getByVideoId(bmsVideo.getId());
        if(bmsUserVideo == null){
            return;
        }
        if(userId.intValue() == bmsUserVideo.getUserId().intValue()){
            if(bmsDxComment.getFromOrigin() == bmsVideo.getFromOrigin()){
                return;
            }
        }
        BmsDxNews bmsDxNews = new BmsDxNews();
        bmsDxNews.setIsDelete(Constants.COMMON_FLAG_FALSE_INT);
        //bmsDxNews.setFromOrigin(bmsVideo.getFromOrigin());
        bmsDxNews.setOriginRole(bmsVideo.getOriginRole());
        bmsDxNews.setChildType(BmsDxNewsService.PARENTTYPT_INT_COMMENT);
        bmsDxNews.setBmsUserId(bmsUserVideo.getUserId());
        bmsDxNews.setBmsBusinessId(bmsVideo.getBmsId());
        bmsDxNews.setOpUserId(userId);
        bmsDxNews.setBmsVideoId(bmsVideo.getId());
        bmsDxNews.setBmsCommentId(bmsDxComment.getId());
        bmsDxNews.setBmsAnswerId(null);
        bmsDxNews.setParentType(BmsDxNewsService.PARENTTYPT_INT);
        bmsDxNews.setContent(bmsDxComment.getContent());
        if(bmsDxComment.getFromOrigin() == Constants.USER_CLIENT) {
            bmsDxNews.setTitile(bmsDxNewsService.getBmsNewsTitle(bmsDxComment.getBmsUserId()));
        }else {
            Optional<BmsBusiness> bmsBusinessOptional = bmsBusinessService.getBaseDao().findById(bmsDxNews.getBmsBusinessId());
            if(bmsBusinessOptional.isPresent()) {
                bmsDxNews.setTitile(bmsBusinessOptional.get().getVendorName());
            }
        }
        bmsDxNewsService.saveNews(bmsDxNews,bmsVideo);
        //加入极光推送
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(AuroraPushCode.USER_DO_COMMENT).getAlert(),bmsDxNews.getTitile());
        BmsCommonService.pushToFront(bmsDxNews,AuroraPushCode.USER_DO_COMMENT,alert);
    }
}
